#!/usr/bin/tclsh

#Name  : template.tcl
#Author: ljgibbs
#Date  : Apr. 2022
#Description :
### Template file for TCL
### Syntax reference:
#https://www.yiibai.com/tcl/tcl_basic_syntax.html

set name DDer
set num 1
puts "Print string"
puts $name

#if-else
#space for { } is necessnary
#can't use non-numeric string as operand of "!"
if { !$num } {
   puts "No engineer" 
} elseif { $num == 1 || $num == 2} { 
   puts "some engineer" 
} else { 
   puts "More engineers" 
}

#switch
switch $name {
    "DDer" { puts "$name = digital designer" }
    "DVer" { puts "$name = design verification"  }
    default { puts "$name , other engineer" }
}

#while
while { $num < 10 } {
    puts "Need [expr 10  - $num] guys"
    incr num 1;# inline comment: num = num + 1
}

#for
for {set index 0} { $index < 3 } { incr index } {
    set num_array(index) $index 
    if { $index == 2 } {
        continue;#same as C
    }
}
puts [array size num_array]

#foreach
set icers  [list "DDer" "DVer" "DFTer"]
foreach icer $icers {
    puts $icer
}

#write file
set var "write tcl file"
set f [ open data.txt w+ ]
puts $f $var
flush $f
close $f 

#read file
set var_rd ""
set f [ open data.txt r ]
gets $f var_rd
close $f
puts $var_rd


#proc 
proc get_name { i } {
    set icers { "DDer" "DVer" "DFTer" }
    set len [llength $icers]
    if { $i >= $len } {
        return " "
    } else {
        return [lindex $icers $i] 
    }
}

#call proc
set i 1
puts [ get_name $i ]



